// 页面路径：store/index.js
import {
	createStore
} from 'vuex'
const store = createStore({
	state: { //存放状态
		url: '',
		realkeyword: '',
		keywords: '',
		// songId: '',
		audio: uni.createInnerAudioContext(),
		detail: null,
		audioSrc: '',
		lyric: [],
		lyrSecond: [],
		lyrIndex: 0,
		alImg: '',
		paused: false,
		mode: 2,
		history: [],
		musicList: [],
		artists: [],
		likeMusic: []
	},
	getters: {
		artistsName: state => {
			let artists = state.detail.ar
			let artistsName = ''
			for (let artist of artists) {
				artistsName += artist.name + '/'
			}
			return artistsName.substring(0, artistsName.length - 1)
		}
	},
	mutations: {
		setRealkeyword(state, realkeyword) {
			state.realkeyword = realkeyword
			//console.log(realkeyword)
		},
		setReywords(state, keywords) {
			state.keywords = keywords
			// console.log(realkeyword)
		},
		// setSongId(state, songId) {
		// 	state.songId = songId
		// },

		//搜索历史
		setHistory(state, history) {
			if (state.history.indexOf(history) === -1) {
				if (state.history.length > 9) {
					state.history.pop()
					state.history.unshift(history)
				} else {
					state.history.unshift(history)
				}

			} else {
				let arr = state.history.filter(word => word !== history);
				// console.log(arr)
				arr.unshift(history)
				// console.log(arr)
				state.history = arr
			}
			uni.setStorage({
				key: 'history',
				data: state.history,
				success: function() {
					// console.log('success');
				}
			});
		},
		restoreHistory(state, history) {
			state.history = history
		},
		removeHistory(state) {
			uni.showModal({
				title: '提示',
				content: '确定清空全部历史记录',
				success: function(res) {
					if (res.confirm) {
						state.history = []
						uni.removeStorage({
							key: 'history',
							success: function(res) {
								console.log('success');
							}
						});
					} else if (res.cancel) {
						return
					}
				}
			});

		},
		play(state) {
			if (state.audioSrc) {
				state.audio.play()
				state.paused = true
			} else {
				uni.showToast({
					title: '该歌曲没有资源',
					duration: 2000,
					icon: 'none'
				});
				state.paused = false
			}
		},
		pause(state) {
			state.audio.pause()
			state.paused = false
		},
		setMode(state) {
			state.mode++;
			if (state.mode === 4) {
				state.mode = 1
			}
		},
		//歌曲详情
		setAlImg(state, img) {
			state.alImg = img
		},
		setAudioSrc(state, audioSrc) {
			state.audioSrc = audioSrc
			if (audioSrc) {
				state.audio.src = audioSrc;
			} else {
				uni.showToast({
					title: '该歌曲没有资源',
					duration: 2000,
					icon: 'none'
				});
				state.paused = false
			}
		},
		setDetail(state, detail) {
			// console.log(detail);
			state.detail = detail
			let index = state.musicList.findIndex(ele => ele.id == state.detail.id)
			state.musicIndex = index
			uni.setStorage({
				key: 'detail',
				data: detail,
				success: function() {
					// console.log('success');
				}
			});
		},
		
		// setArtistName(state, artist) {
		// 	let artistName = ''
		// 	for (let item of artist) {
		// 		artistName += item.name + '/'
		// 	}
		// 	return artistName.substring(0, artistName.length - 1)
		// },
		setMusicList(state, detail) {
			let index = state.musicList.findIndex(ele => ele.id == state.detail.id)
			// console.log(index);
			let i = state.musicList.findIndex(ele => ele.id == detail.id)
			// console.log(i)
			if (i === -1) {
				state.musicList.splice(index + 1, 0, detail)
				// console.log(state.musicList)
				let artists = detail.ar
				let artistsName = ''
				for (let artist of artists) {
					artistsName += artist.name + '/'
				}
				let a = artistsName.substring(0, artistsName.length - 1)
				state.artists.splice(index + 1, 0, a)
				// console.log(state.artists)
				uni.setStorage({
					key: 'musicList',
					data: state.musicList,
					success: function() {
						// console.log('success');
					}
				});
				uni.setStorage({
					key: 'artists',
					data: state.artists,
					success: function() {
						// console.log('success');
					}
				});
			}
		},
		changeMusicList(state, musicList) {
			state.musicList = musicList
			uni.setStorage({
				key: 'musicList',
				data: state.musicList,
				success: function() {
					// console.log('success');
				}
			});
		},
		changeArtists(state, artists) {
			state.artists = artists
			uni.setStorage({
				key: 'artists',
				data: state.artists,
				success: function() {
					// console.log('success');
				}
			});
		},
		//喜欢的音乐
		setLikeMusic(state, detail) {
			state.likeMusic.push(detail)
			// console.log(state.likeMusic);
			uni.setStorage({
				key: 'likeMusic',
				data: state.likeMusic,
				success: function() {
					// console.log('success');
				}
			});
		},
		cutLikeMusic(state, detail) {
			let i = state.likeMusic.findIndex(ele => ele.id == detail.id)
			// console.log(i);
			state.likeMusic.splice(i, 1)
			// console.log(state.likeMusic);
			uni.setStorage({
				key: 'likeMusic',
				data: state.likeMusic,
				success: function() {
					// console.log('success');
				}
			});
		},
		changeLikeMusic(state, likeMusic) {
			state.likeMusic = likeMusic
		},
		setMode(state) {
			state.mode++
			if (state.mode > 3) {
				state.mode = 1
			}
		},
		setLyric(state,lyric){
			state.lyric = lyric
		},
		setLyrSecond(state,lyrSecond){
			state.lyrSecond = lyrSecond
		}
		// setSecond(state,str) {
		// 	let arr = str.split(':')
		// 	return arr[0] * 60 + arr[1] * 1
		// },
		// merge(state,detail){
		// 	let artists = detail.artists 
		// 	let artistsName = ''
		// 	for (let artist of artists) {
		// 		artistsName += artist.name + '/'
		// 	}
		// 	return artistsName.substring(0, artistsName.length - 1)
		// }
	},
	actions: {
		//获取默认搜索关键词
		getRealkeyword({
			commit,
			state
		}) {
			if (!state.realkeyword) {
				uni
					.request({
						url: state.url + "/search/default"
					})
					.then((res) => {
						// console.log(res.data);
						// commit('setKeyword',res.data.data.realkeyword)
						commit('setRealkeyword', res.data.data.realkeyword)
					})
					.catch((err) => {
						console.error(err);
					});
			}
		},
		//获取歌曲src,并播放
		getAudioSrc({
			commit,
			state
		}, songId) {
			uni
				.request({
					url: state.url + `/song/url?id=${songId}`
				})
				.then((res) => {
					// console.log(res.data.data[0].url);
					commit('setAudioSrc', res.data.data[0].url)
					if (res.data.data[0].url) {
						commit('play')
					} else {
						commit('pause')
					}

				})
				.catch((err) => {
					console.error(err);
				});
		},
		//删除音乐列表的歌曲
		cutMusicList({
			commit,
			state
		}, i) {
			let index = state.musicList.findIndex(ele => state.detail.id == ele.id)
			state.musicList.splice(i, 1)
			state.artists.splice(i, 1)

			if (index == i) {
				if (state.musicList[i]) {
					commit('setDetail', state.musicList[i])
				} else {
					if (state.musicList[0]) {
						commit('setDetail', state.musicList[0])
					} else {
						commit('setDetail', null)
						uni.navigateTo({
							url: '/pages/index/index'
						});
					}
				}
				if (state.detail) {
					uni
						.request({
							url: state.url + `/song/url?id=${state.detail.id}`
						})
						.then((res) => {
							commit('setAudioSrc', res.data.data[0].url)
							if (res.data.data[0].url) {
								commit('play')
							} else {
								commit('pause')
							}
						})
						.catch((err) => {
							console.error(err);
						});
				} else {
					commit('pause')
				}
			}


			uni.setStorage({
				key: 'musicList',
				data: state.musicList,
				success: function() {
					// console.log('success');
				}
			});
			uni.setStorage({
				key: 'artists',
				data: state.artists,
				success: function() {
					// console.log('success');
				}
			});
		},
		//获取歌词
		getLyric({commit,state},id){
			uni.request({
				url: state.url + `/lyric?id=${id}`,
				success: (res) => {
					// console.log(res.data.lrc.lyric);
					let lrc = res.data.lrc.lyric.split('[')
					// console.log(lrc);
					lrc.shift()
					let lyric=[]
					let lyrSecond=[]
					for (let item of lrc) {
						let l = item.split(']')
						// console.log(l[0]);
						let arr = l[0].split(':')
						let second = arr[0] * 60 + arr[1] * 1
						// console.log(second);
						lyric.push(l[1])
						lyrSecond.push(second)
					}
					commit('setLyric',lyric)
					commit('setLyrSecond',lyrSecond)
					// console.log(this.lyric, this.second);
				}
			})
		},
	}
})

export default store
